package com.whksoft.mymapplugin.Listener;

import com.whksoft.mymapplugin.utils.flagUtils;
import org.bukkit.NamespacedKey;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;

import static com.whksoft.mymapplugin.utils.flagUtils.haveQRFlag;

/**
 * 检测玩家丢弃物品
 */
public class throwItems implements Listener {

    Plugin plugin;

    public throwItems(Plugin plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void throwItem(PlayerDropItemEvent e) {
        //FIXME:
        // 已有bug:创造模式下会导致数目翻倍
        NamespacedKey key = new NamespacedKey(plugin, "isQRCode");
        ItemStack itemStack = e.getItemDrop().getItemStack();
        ItemMeta itemMeta = itemStack.getItemMeta();
        assert itemMeta != null;
        if (haveQRFlag(key, itemStack)) {
            //e.getPlayer().sendMessage("你丢你妈呢？");
            e.setCancelled(true);
            itemStack.setAmount(1);
        }
    }

}
